#include <iostream>
#include <string>
#include <sys/socket.h>
#include <sys/types.h>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <errno.h>
#include <netinet/in.h>
//#include <netdb.h>

#define BUFFER_SIZE 1024
#define PORT        8100

using namespace std;

int main() {
  
  int _socket, bytes_recieved;
  char send_buffer[BUFFER_SIZE], recv_buffer[BUFFER_SIZE];
  
  //  struct hostent * host;
  struct sockaddr_in server;
  //host = gethostbyname("127.0.0.1");
  
  _socket = socket(PF_UNIX, SOCK_STREAM, 0);

  if (_socket == -1) {
    perror("socket()");
    exit(1);
  }
  
  server.sin_family = PF_UNIX;
  server.sin_port = htons(PORT);
  server.sin_addr.s_addr = INADDR_ANY; //*((struct in_addr *) host->h_addr);
  bzero(&(server.sin_zero), 8);
  
  if(connect(_socket, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1) {
    perror("connect()");
    exit(1);
  }
  
  while (1) {    
    bytes_recieved = recv(_socket, recv_buffer, BUFFER_SIZE, 0);
    recv_buffer[bytes_recieved] = '\0';
  
    printf("\nRecieved data = %s " , recv_buffer);
  
    printf("\nSEND (q or Q to quit) : ");
    cin.getline(send_buffer, BUFFER_SIZE);
  
    send(_socket, send_buffer, strlen(send_buffer), 0);
    if (strcmp(send_buffer, "q") == 0 || strcmp(send_buffer, "Q") == 0)
      break;
  }
  close(_socket);
}
